// 获取全局 app 实例
const app = getApp();

Page({
  data: {
    loading:false,
    username: "",
    password: "",
  },
  onLoad() {},


  onSubmit(e) {

    var data = e.detail.value;

    //输入账号判断
    if (data.username == "" || data.password == "") {
      my.showToast({
        type: 'fail',
        content: "请输入账号和密码!"
      });
      return false;
    }

     let that = this;
      my.showLoading({
        content: '加载中...'
      });
      //请求数据
      this.setData({
        loading:true
      }),
      my.request({
      url: 'https://tiyi.site/seaHorse/sys/login',
      method: 'POST',
      data: {
          username:data.username,
          password:data.password
      },
      dataType: 'json',
      success: function(res) {
        that.setData({
          loading:false
        });
     
        if(res.data.code == 0){
          my.showToast({
          type: 'success',
          content: res.data.msg
          });
          //成功
          my.setStorageSync({
            key: 'token',
            data:res.data.token
          })

          my.switchTab({
            url: '/pages/index/index'
          })
        }else{
          my.showToast({
          type: 'fail',
          content: res.data.msg
          });
        }
       
      },
      fail: function(res) {
        my.alert({content: res});
      },
      complete: function(res) {
        my.hideLoading();
      }
      });
  },

});
